<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/verfy.jsp"%>
<%
	// 先查科室
	List<Hospital> hospitalList = QBuilder.db("basedb").select().from("Hospital").where(new Where("hospital=", hospital)).query().getResultObj();
	if (isEmpty(hospitalList)) {
		Tools.encryptPrint(out, new Message(30041, "不支持该医院的预约挂号"));
		return;
	}
	Hospital hospitalObj = hospitalList.get(0);
	List<Map> depts = QBuilder.db("basedb").select("new map(id as id, deptName as title, branch as branch, (case when summary is NULL then '' else summary end) as summary, (case when img is NULL then '' else img end) as img, (case when content is NULL then '' else content end) as content, (case when imgs is NULL then '' else imgs end) as imgs)").from("Dept").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("type=0")).query().getResultObj();
	if (isEmpty(depts)) {
		Tools.encryptPrint(out, new Message(30041, "暂无排班信息"));
		return;
	}
	Map map = depts.get(0);
	String deptId = input.getDesc("dept_id", Tools.getDesc(map.get("id"), "-1"));
    String branch = input.getDesc("branch", "-1");
	Where where = new Where();
	if (!"-1".equals(branch))
		where.and("s.branch=", branch);
	List<Map> expertIds = QBuilder.db("basedb").select("new map(d.id as id, d.deptId as dept_id, s.dept_type as dept_type, d.deptName as dept_name)").from("dept_link as s, Dept as d").where(new Where(where).and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE).and("s.dept_id=", deptId).and("d.deptId=s.other_id")).query().getResultObj();
	if (isEmpty(expertIds)) {
		Tools.encryptPrint(out, new Message(30041, "暂无排班信息"));
		return;
	}
	// 分离普通号、专家号
	String origin = "'-1'";
	String normalDeptId = origin;
	String expertDeptId = origin;
	boolean isReal = "1".equals(checkSource(application.getRealPath("/") + appType + "/" + hospital + "/conf.xml"));// 1代表实时号
	List<Map> normal = new ArrayList<Map>();   // 存放普通号
	List<String> dates = new ArrayList<String>();
	for (int i = 0; i < expertIds.size(); i++) {
		Map expertId = expertIds.get(i);
		String deptType = Tools.getDesc(expertId.get("dept_type"), "");
		String deptOtherId = Tools.getDesc(expertId.get("dept_id"), "");
		if ("".equals(deptOtherId)) continue;
		if (deptType.equals(DEPT_ORDINARY+"")) {
			if (isReal) {    // 如果是实时号
				String date = DateUtil.formatDate(new Date());
				expertId.put("real", "1");
				expertId.put("date", date);
				normal.add(expertId);
				if (!dates.contains(date)) {
					dates.add(date);
				}
			} else {
				normalDeptId += ",'" + deptOtherId + "'";
			}
		} else if (deptType.equals(DEPT_EXPERT+"")) {
			expertDeptId += ",'" + deptOtherId + "'";
		}
	}
	Map resultMap = new HashMap();
	if (!origin.equals(normalDeptId)) {   // 有普通号排班存在
		normal = QBuilder.db("basedb").select("new map(s.id as id, s.doctorId as expert_id, s.doctorName as name, s.deptId as dept_id, s.deptName as dept_name, s.date as date)").from("Schedule as s").where(new Where(where).and("s.deptId in("+ normalDeptId +")").and("s.registerType=1").and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE)).query().getResultObj();
	}
	List<Map> result = new ArrayList<Map>();
	if (!origin.equals(expertDeptId)) {   // 有专家号排班存在
		// 查询排班表中专家排班
		List<Map> schedules = QBuilder.db("basedb").select("new map(s.id as id, s.doctorId as expert_id, s.doctorName as name, s.deptId as dept_id, s.deptName as dept_name, s.date as date, s.registryFee as registry_fee)").from("Schedule as s").where(new Where(where).and("s.deptId in("+expertDeptId+")").and("s.registerType=2").and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE)).query().getResultObj();
		// 查出排班获取医生信息
		if (!isEmpty(schedules)) {
			String expertId = "'-1'";
			for (int i = 0; i < schedules.size(); i++) {
				Map _map = schedules.get(i);
				expertId += ",'" + Tools.getDesc(_map.get("expert_id"), "-1") + "'";
				String date = Tools.getDesc(_map.get("date"), DateUtil.formatDate(new Date()));
				if (!dates.contains(date)) {
					dates.add(date);
				}
			}
			List<Map> experts = QBuilder.db("basedb").select("new map(e.id as id, e.name as name, (case when e.expertId is NULL then '' else e.expertId end) as expert_id, (case when e.avatar is NULL then '' else e.avatar end) as avatar, (case when e.specialty is NULL then '' else e.specialty end) as specialty, (case when e.title is NULL then '' else e.title end) as title)").from("Expert e").where(new Where("e.hospital=", hospital).and("e.status = ", STATUS_ENABLE).and("e.expertId in("+expertId+")")).query().getResultObj();
			for (int i = 0; i < schedules.size(); i++) {
				Map _map = new HashMap();
				Map tempMap = schedules.get(i);
				String doctorId = Tools.getDesc(tempMap.get("expert_id"), "-1");
				_map.put("expert_id", doctorId);
				_map.put("avatar", DEFAULT_AVATAR);
				_map.put("name", Tools.getDesc(tempMap.get("name"), ""));
				_map.put("date", Tools.getDesc(tempMap.get("date"), ""));
				_map.put("registry_fee", Tools.getDesc(tempMap.get("registry_fee"), ""));
				_map.put("dept_name", Tools.getDesc(tempMap.get("dept_name"), ""));
				_map.put("hospital_name", hospitalObj.getName());
				for (int j = 0; j < experts.size(); j++) {
					Map _tempMap = experts.get(j);
					String _doctorId = (String)_tempMap.get("expert_id");
					if (doctorId.equals(_doctorId)) {
						_map.put("id", Tools.getDesc(_tempMap.get("id"), ""));
						_map.put("avatar", Tools.getDesc(_tempMap.get("avatar"), ""));
						_map.put("title", Tools.getDesc(_tempMap.get("title"), ""));
						_map.put("specialty", Tools.getDesc(_tempMap.get("specialty"), ""));
					}
				}
				result.add(_map);
			}	
		}
	}
	for (int i = 0; i < normal.size(); i ++) {
		Map _map = normal.get(i);
		String date = Tools.getDesc(_map.get("date"), DateUtil.formatDate(new Date()));
		if (!dates.contains(date)) {
			dates.add(date);
		}
	}
	Collections.sort(dates);
	List<Map> data = new ArrayList<Map>();
	Map _result = new HashMap();
	List<Map> allexperts = new ArrayList<Map>();
	List<String> allexpertIds = new ArrayList<String>();
	for (int i = 0; i < dates.size(); i++) {
		String date = dates.get(i);
		Map _map = new HashMap();
		data.add(_map);
		_map.put("date", date);
		// 获取当天普通号
		List<Map> _normal = (List<Map>)_map.get("normal");
		if (_normal == null) {
			_normal = new ArrayList<Map>();
			_map.put("normal", _normal);
		}
		Iterator<Map> ite = normal.iterator();
		while (ite.hasNext()) {
			Map m = ite.next();
			String _date = Tools.getDesc(m.get("date"));
			if (date.equals(_date)) {
				_normal.add(m);
				ite.remove();
			}
		}
		
		// 获取当天医生号
		List<Map> _expert = (List<Map>)_map.get("expert");
		if (_expert == null) {
			_expert = new ArrayList<Map>();
			_map.put("expert", _expert);
		}
		ite = result.iterator();
		while (ite.hasNext()) {
			Map m = ite.next();
			String _date = Tools.getDesc(m.get("date"));
			String expertId = Tools.getDesc(m.get("expert_id"));
			if (!allexpertIds.contains(expertId)) {
				allexperts.add(m);
				allexpertIds.add(expertId);
			}
			if (date.equals(_date)) {
				_expert.add(m);
				ite.remove();
			}
		}
	}
	Map whole = new HashMap();
	whole.put("normal", normal);
	whole.put("expert", allexperts);
	_result.put("whole", whole);
	// 为某天专家上下午皆出诊时去重显示
	for (Map expertMap:data) {
		List<Map> expertList = (List<Map>)expertMap.get("expert");
		List<Map> tempList = new ArrayList<>();
		Iterator<Map> it = expertList.iterator();  
		while(it.hasNext()){  
			Map itMap = it.next();
			boolean isEx = false;
			String doctorId = Tools.getDesc(itMap.get("expert_id"));
			for (Map expert:tempList) {
				String doctorId2 = Tools.getDesc(expert.get("expert_id"));
				if (doctorId2.equals(doctorId))
					isEx = true;
			}
			if(isEx){  
				it.remove();  
			}  
			else{  
				tempList.add(itMap);  
			}  
		} 
	}
	_result.put("day", data);
	msg.put("data", _result);
	Tools.encryptPrint(out, msg);
%>